package com.ksm.zyz.controller;


import com.ksm.zyz.sys.entity.Combobox;
import com.ksm.zyz.common.base.BaseController;
import com.ksm.zyz.common.base.Global;
import com.ksm.zyz.common.util.EncodeUtil;
import com.ksm.zyz.sys.entity.ExtTree;
import com.ksm.zyz.sys.entity.Message;
import com.ksm.zyz.sys.entity.SysUser;
import com.ksm.zyz.sys.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

/**
 * @author litao
 *
 */
@Controller
public class LoginController extends BaseController<Object> {

	@Autowired
	private SysUserService sysUserService;

	/**
	 * 登录验证
	 *
	 * @return
	 */
	@RequestMapping(value = "login")
	@ResponseBody
	public Message loginAction(String loginName,String password){
		SysUser sysUser ;
		if(loginName!=null && password!=null){
			sysUser = sysUserService.checkLogin(loginName, EncodeUtil.MD5Encode(password));
			if(sysUser!=null){
				session.setAttribute(Global.CURRENT_USER,sysUser);
				logger.info("{}--->{},登录成功。。。",sysUser.getLoginName(),sysUser.getRealName());
				logger.info("{}",sysUser.toString());
				return new Message("sys.do");
			}else {
				return new Message(false,"用户名或密码错误");
			}
		}
		return new Message(false,"系统错误请联系管理员");
	}

	@RequestMapping("quit")
	public String loginOut(){
		session.removeAttribute(Global.CURRENT_USER);
		session.invalidate();
		return "redirect:index.do";
	}

	/**
	 * 查询树形菜单
	 * @return
	 */
	@RequestMapping("getPermission")
	@ResponseBody
	public List<ExtTree> getPermission(){
		SysUser sysUser = getUserInfo();
		if(sysUser != null && sysUser.getRoleId() != null){
			//树形菜单组合算法
			List<ExtTree> jsonList = sysUserService.findMenuInfo(sysUser.getRoleId());
			List<ExtTree> list = new ArrayList<ExtTree>();
			for(int i=0;i<jsonList.size();i++){
				String id = jsonList.get(i).getId();
				for(ExtTree j2 : jsonList){
					if(id.equals(j2.getParentKey())){
						jsonList.get(i).setLeaf(false);
						j2.setLeaf(true);
						jsonList.get(i).getChildren().add(j2);
					}
				}
			}
			for(ExtTree j2 : jsonList){
				String key = j2.getParentKey();
				if(key==null||"".equals(key)){
					list.add(j2);
				}
			}
			return list;
		}
		return null;
	}

	@RequestMapping("checkSession")
	@ResponseBody
	public Object checkLogin(){
		SysUser sysUser = getUserInfo();
		if(null==sysUser){
			return new Message(false,Global.FAILURE);
		}else{
			return new Message(Global.SUCCESSFUL);
		}
	}

	/**
	 * 查询登录名是否重复
	 * @param sysUser
	 * @return
	 */
	@RequestMapping("checkLoginName")
	@ResponseBody
	public Message checkLoginName(SysUser sysUser){
		int check = sysUserService.checkLoginName(sysUser);
		if(check==0){
			return new Message(Global.SUCCESSFUL);
		}else{
			return new Message(false,Global.FAILURE);
		}
	}


	/**
	 * 获取下拉
	 * @param label
	 * @return
	 */
	@RequestMapping("getCombobox")
	@ResponseBody
	public List<Combobox> getCombobox(String label){
		List<Combobox> comlist = sysUserService.getCombobox(label);
		return comlist;
	}
}
